package com.qfedu.product.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qfedu.product.dao.impl.sellDaoimpl;
import com.qfedu.product.po.Check;
import com.qfedu.product.po.Inventory;
import com.qfedu.product.po.Product;
import com.qfedu.product.po.SelectVip;
import com.qfedu.product.service.sellService;
import com.qfedu.product.service.impl.sellServiceimpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * Servlet implementation class sellServlet
 */
@WebServlet("/sellServlet")
public class sellServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public sellServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO
		// 接收商品的信息：商品编号、销售数量、手机号码
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/javascript");
		
		PrintWriter writer = response.getWriter();
		String method = request.getParameter("method");
		sellService sellService = new sellServiceimpl();
		sellDaoimpl sellDaoimpl=new sellDaoimpl();
		List<Product> list = null;
		// System.out.println(method);

		switch (method) {
		case "sell":
			String proid = request.getParameter("proid");
			String proType = request.getParameter("proType");
			String proName = request.getParameter("proName");
			String proNum = request.getParameter("proNum");
			String proPrice = request.getParameter("proPrice");
			String phoneNum = request.getParameter("phoneNum");
			String proSumPrice = request.getParameter("proSumPrice");
			String user_id = "admin";
			Boolean boolResult = sellService.doSell(new Object[] { proid, proType, proName, proNum, proPrice, phoneNum, proSumPrice, user_id });
			writer.print(boolResult);
			writer.flush();
			break;

		case "sell_select":
			String proid1 = request.getParameter("proid");
			String proNum1 = request.getParameter("proNum");
			String phoneNum1 = request.getParameter("phoneNum");
			list = sellService.sellSelect(new Object[] { proid1, proNum1, phoneNum1 });
			request.setAttribute("list", list);
			request.getRequestDispatcher("sell_select.jsp").forward(request, response);
			break;
		case "check":
			//返回类别，名称，单价
			String a = request.getParameter("proid");
			Check check = sellDaoimpl.docheck(new Object[] {a});
			//System.out.println(check);
			writer.print(JSONObject.fromObject(check));
			break;
		case "selectVip":
			String b = request.getParameter("phoneNum");
			boolean bool = sellService.doVip(new Object[] {b});
			writer.print(bool);
			System.out.println(bool);
			Integer proNum2 = Integer.parseInt(request.getParameter("proNum"));
			String proid2 = request.getParameter("proid");
			
			System.out.println(proNum2 + "这是什么"+sellDaoimpl.dosum(new Object[] {proid2}));
			
			break;
		case "showTable":
			String c = request.getParameter("proid");
			//list = sellService.showTable(new Object[] {c});
			request.setAttribute("list", list);
			request.getRequestDispatcher("Sell_list.jsp").forward(request, response);
			break;

		default:
			break;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
